using FreeSql.DataAnnotations;

namespace SumerCoreDevOps.Domain.Entities;

/// <summary>
/// 构建历史记录实体
/// </summary>
[Table(Name = "build_histories")]
public class BuildHistory
{
    /// <summary>
    /// 历史记录 ID
    /// </summary>
    [Column(IsPrimary = true, IsIdentity = true)]
    public long Id { get; set; }

    /// <summary>
    /// 关联的流水线实例 ID
    /// </summary>
    [Column(IsNullable = false)]
    public long PipelineInstanceId { get; set; }

    /// <summary>
    /// Git Tag（语义化版本）
    /// 例如：v1.2.3-prod
    /// </summary>
    [Column(StringLength = 100)]
    public string? GitTag { get; set; }

    /// <summary>
    /// Git Commit SHA（完整版本）
    /// </summary>
    [Column(StringLength = 100, IsNullable = false)]
    public string CommitSha { get; set; } = null!;

    /// <summary>
    /// Git Commit SHA（短版本，前 8 位）
    /// </summary>
    [Column(StringLength = 20, IsNullable = false)]
    public string CommitShortSha { get; set; } = null!;

    /// <summary>
    /// Git 分支名
    /// </summary>
    [Column(StringLength = 200)]
    public string? BranchName { get; set; }

    /// <summary>
    /// 触发类型：Push、Tag、PullRequest、Manual
    /// </summary>
    [Column(StringLength = 50, IsNullable = false)]
    public string TriggerType { get; set; } = null!;

    /// <summary>
    /// 触发用户
    /// </summary>
    [Column(StringLength = 100)]
    public string? TriggerUser { get; set; }

    /// <summary>
    /// 执行节点 ID
    /// </summary>
    [Column(StringLength = 100)]
    public string? ExecuteNodeId { get; set; }

    /// <summary>
    /// Docker 镜像完整标签
    /// 例如：harbor.example.com/auth-service:v1.0.0-dotnet-7f3a9d2b
    /// </summary>
    [Column(StringLength = 1000)]
    public string? DockerImageTag { get; set; }

    /// <summary>
    /// Docker 镜像 Digest（SHA256）
    /// </summary>
    [Column(StringLength = 200)]
    public string? ImageDigest { get; set; }

    /// <summary>
    /// 镜像大小（字节）
    /// </summary>
    public long? ImageSize { get; set; }

    /// <summary>
    /// 构建语言/平台
    /// </summary>
    [Column(StringLength = 50)]
    public string? BuildLanguage { get; set; }

    /// <summary>
    /// 环境标识
    /// </summary>
    [Column(StringLength = 50)]
    public string? Environment { get; set; }

    /// <summary>
    /// 构建状态：Building、Success、Failed、Cancelled
    /// </summary>
    [Column(StringLength = 50, IsNullable = false)]
    public string BuildStatus { get; set; } = "Building";

    /// <summary>
    /// 构建开始时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime StartTime { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 构建结束时间
    /// </summary>
    public DateTime? EndTime { get; set; }

    /// <summary>
    /// 构建耗时（秒）
    /// </summary>
    public int? DurationSeconds { get; set; }

    /// <summary>
    /// 构建产物路径
    /// </summary>
    [Column(StringLength = 1000)]
    public string? ArtifactPath { get; set; }

    /// <summary>
    /// 部署版本（K8s Deployment 版本）
    /// </summary>
    [Column(StringLength = 200)]
    public string? DeploymentVersion { get; set; }

    /// <summary>
    /// 元数据（JSON 格式，存储额外信息）
    /// </summary>
    [Column(StringLength = -1)]
    public string? Metadata { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 关联的流水线实例
    /// </summary>
    [Navigate(nameof(PipelineInstanceId))]
    public PipelineInstance? PipelineInstance { get; set; }
}
